<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2020-08-01
 * Time: 下午 08:38
 */
namespace Lib;

/**
 * Class Action
 * @package Lib
 * 
 * @property-read \Lib\Core $core
 * @property-read \Lib\Functions $fns
 */
abstract class Action extends Injector {
    public static function getActionStr() {
        $class = static::class;
        return str_replace('\\', '.', trim(preg_replace('#^Action#', '', $class), '/\\'));
    }
    
    public static function getActionUrl($params=[]) {
        /** @var Functions $fns */
        $fns = di()->get(Functions::class);
        return $fns->newUrlCreator('/')
            ->mergeQueryParams($params)
            ->setQueryParams('a', self::getActionStr())
            ->toString()
        ;
            
    }
    
    public function main() {
        $this->view();
    }
    
    public function view($view='', $last=true) {
        if(!$view) {
            $view = $this->_selfView();
        }
        
        $path = $this->core->viewPath("$view.phtml");

        if(!file_exists($path) && $view != '404') {
            $path = $this->core->viewPath('404');
        }

        if(!file_exists($path)) {
            if(!$last) {
                return ;
            }
            
            $this->core->actionNotfound();
            exit;
        }
        
        if($last) {
            header('Content-Type: text/html');
            include $path;
            exit;
        } else {
            include $path;
        }
    }
    
    public function notfound() {
        $this->core->actionNotfound();
    }
    
    protected function _selfView() {
        $class = get_class($this);
        $classAry = explode('\\', $class);
        
        array_shift($classAry);
        $action = array_pop($classAry);
        
        $viewName = strtolower(ltrim(preg_replace('#([A-Z])#', '-$1', preg_replace('#Action$#', '', $action)), '-'));
        
        if(empty($classAry)) {
            $viewPath = $viewName;
        } else {
            $viewPath = implode('/', $classAry) . '/' . $viewName;
        }
        
        return strtolower($viewPath);
    }
    
    public function setMessage($msg) {
        $_SESSION['message'] = $msg;
    }
    
    public function setMessageData($data) {
        $_SESSION['message_data'] = $data;
    }
    
    public function readMessage() {
        $msg = $_SESSION['message'];
        $_SESSION['message'] = '';
        return $msg;
    }

    public function readMessageData() {
        $data = $_SESSION['message_data'];
        $_SESSION['message_data'] = '';
        return $data;
    }
    
    public function hasMessage() {
        return !empty($_SESSION['message']);
    }

    public function jsBackAlert($msg) {
        $msg = str_replace("\n", '\\n', $msg);
        $msg = str_replace("'", "\\'", $msg);
        
        echo "<script type='text/javascript'>
window.alert('$msg');
window.history.back();
</script>";
        die;
    }
    
    public function jsBack() {
        echo "<script type='text/javascript'>window.history.back()</script>";
        die;
    }
    
    public function redirect($url) {
        header('location: ' . $url);
        exit;
    }
}